﻿@model IEnumerable<Kooboo.CMS.Content.Models.TextContent>
@{
    Layout = "~/Areas/" + Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.FoodOrderingAreaRegistration.ModuleName + "/Views/Shared/Front.cshtml";

    var loginModel = Kooboo.CMS.FoodOrdering.Areas.Customer.Models.LoginModel.Current;

    string cata = Request.QueryString["type"];
    string date = Request.Form["search_ordertime"];
    string meal = Request.Form["search_mealtime"];
    if (string.IsNullOrEmpty(cata))
    {
        cata = "0";
    }
    if (string.IsNullOrEmpty(meal))
    {
        meal = (DateTime.Now.Hour <= 12 ? "1" : "2");
    }
    if (string.IsNullOrEmpty(date))
    {
        date = DateTime.Now.ToString("yyyyMMdd");
    }
    var ordered = ContentHelper.TextFolder(Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.FoodOrderingAreaRegistration.OrderDetailFolderName).CreateQuery().WhereEquals("Published", true).WhereEquals("OrderTime", date).WhereEquals("Meal", meal);
    var customers = ContentHelper.TextFolder(Kooboo.CMS.FoodOrdering.Areas.Customer.CustomerAreaRegistration.CustomerFolderName).CreateQuery().WhereEquals("Published", true);

}
@section headline{
    <div class="search">
        @using (Html.ModuleHtml().BeginForm("Statistics", "Home", ViewContext.RequestContext.AllRouteValues(), FormMethod.Post))
        { 
            <p class="watermark">
                <span style="color: #555555; font-weight: normal">@("DATE(YYYYMMDD)".Label())</span>
                <input id="search_ordertime" type="text" tabindex="1" value="@date" name="search_ordertime" />
            </p>
            
            <input type="radio" name="search_mealtime" id="search_lunch" value="1" @if (meal == "1")
                                                                                   {<text>checked="checked"</text>}  />@("Lunch".Label())
            <input type="radio" name="search_mealtime" id="search_supper" value="2"@if (meal == "2")
                                                                                   {<text>checked="checked"</text>} />@("Supper".Label())
            <button type="submit">
                @("Search".Label())</button>
        }
    </div>
}
<div class="statistics">
    <dl>
        <dt>@("Headcount".Label())</dt>
        <dd>@customers.Count()</dd>
        <dt>@("Order quantity".Label())
        </dt>
        <dd>@ordered.Count()</dd>
        <dt>@("Total price".Label())
        </dt>
        <dd>@ordered.Sum(i => decimal.Parse(i["Price"].ToString()))</dd>
    </dl>
    <label id="miss">@("Absentee".Label())</label>
    <ul>
        @foreach (var user in customers)
        {
            bool noName = true; foreach (var order in ordered)
            {
                if (user["Name"].ToString()
                    == order["User"].ToString()) { noName = false; break; }
            } if (noName)
            { <li>@user["Name"]</li>
            }
        }
    </ul>
</div>
@if (cata == "0")
{
    var orderByName = ordered.OrderBy(o => o["User"]);
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>
                        @("User".Label())
                    </th>
                    <th>
                        @("Restaurant".Label())
                    </th>
                    <th>
                        @("Food".Label())
                    </th>
                    <th>
                        @("Price".Label())
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var order in orderByName)
                { <tr>
                    <td>
                        @order["User"]
                    </td>
                    <td>
                        @if (Site.Current.Name == "EnglishSite")
                        { @order["Restaurant_En"] }
                        else
                        { @order["Restaurant"] }
                    </td>
                    <td>
                        @if (Site.Current.Name == "EnglishSite")
                        { @order["Food_En"] }
                        else
                        { @order["Food"] }
                    </td>
                    <td>
                        @order["Price"]
                    </td>
                </tr>
                }
            </tbody>
        </table>
    </div> }
else if (cata == "1")
{
    var orderByFood = ordered.GroupBy(i
        => new
        {
            Restaurant = i["Restaurant"].ToString().Trim(),
            Food = i["Food"].ToString().Trim(),
        }).Select(g => new
        {
            Restaurant = g.Key.Restaurant,
            Food = g.Key.Food,
            Count = g.Count(),
            Total = g.Sum(i => decimal.Parse(i["Price"].ToString()))
        }).OrderBy(o => o.Restaurant);
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>
                        @("Restaurant".Label())
                    </th>
                    <th>
                        @("Food".Label())
                    </th>
                    <th>
                        @("Amount".Label())
                    </th>
                    <th>
                        @("Total".Label())
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var order in orderByFood)
                { <tr>
                    <td>
                        @order.Restaurant
                    </td>
                    <td>
                        @order.Food
                    </td>
                    <td>
                        @order.Count
                    </td>
                    <td>
                        @order.Total
                    </td>
                </tr>
                }
            </tbody>
        </table>
    </div> }
@section sidebar{
    <div class="block menubar">
        <h4 class="title">
            @("STATISTICS TYPE".Label())</h4>
        <ul class="list">
            <li><a href="@Url.ModuleUrl().Action("Statistics",
ViewContext.RequestContext.AllRouteValues().Merge("type", "0"))">@("By Account".Label())
            </a></li>
            <li><a href="@Url.ModuleUrl().Action("Statistics", ViewContext.RequestContext.AllRouteValues().Merge("type",
"1"))">@("By Food".Label())
            </a></li>
        </ul>
    </div>
}
